#include <stdio.h>
#include <unistd.h>
#include "pthread_demo1.h"
#include "../../app/app_Common.h"

/*线程函数*/
static void* pthread_sqrt(void *args)
{
    sleep(1);
    int* nmu_p = (int*)args;
    int num = *nmu_p;
    int sqrt = num * num;
    int* sqrt_p = (int*)malloc(sizeof(int));
    *sqrt_p = sqrt;
    return sqrt_p;
}
int main(void)
{
    int sum = 0;
    int sqrt[5] = {0};
    int arr[5] = {1,2,3,4,5};
    pthread_t pthread_arr[5];
    /*获取开始时间戳*/
    long start = App_Common_get_Currenttime();
    /*1.计算平方[串行]*/
    for (int i = 0; i < 5; i++)
    {
      pthread_create(&pthread_arr[i],NULL,pthread_sqrt,&arr[i]);
    }
    for (int i = 0; i < 5; i++)
    {
        void* res;
        pthread_join(pthread_arr[i],&res);
        int* sqrt_p = (int*)res;
        sqrt[i] = *sqrt_p;
    }
    
    /*计算平方和[串行]*/
    for (int i = 0; i < 5; i++)
    {
        sum += sqrt[i];
    }
    /*获取结束时间*/
    long end = App_Common_get_Currenttime();
    /*打印结果以及过程消耗的时间*/
    printf("sum=%d  time=%ld\n",sum,end - start);

    return 0;
}